define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/searchBlock",
	"EasyUI","EasyUI-lang"
    ],function(Util,Tool,SearchBlock,Theme){

    var sysPath =  getServer() + "/static/app/fwsjcj";

    var zwFormHtml = sysPath + "/brick/views/zwForm.html";
	var getZwController = function(){
		return getServer() + "/platform/org/zws/";
	};

	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
		createZwGrid();
        //浏览器窗口大小变化后，表格宽度自适应
        $(window).resize(function(){
            createZwGrid();
        });

	};
	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(){
        $("#btnSearch").click(function () {
            createZwGrid();
        });
        $("#btnReset").click(function () {
            $("#zwName").val("");
            $("#zwCode").val("");
        });
	};
	var createZwGrid = function() {
        $("#zwGrid").datagrid({
            url:getZwController() + "queryZws",
			method:"GET",
            fitColumns: true,
            autoRowHeight: false,
            columns:[[
				{field:"ck",checkbox:true},
                {field:'zwName',title:'职务名称',width:'50%',align:'center',
                    formatter:function(value,rowData,rowIndex){
                        return '<a href="#" onclick="_editZw(\'' + rowData.zwId+ '\');"> '+rowData.zwName+' </a> '

                    }
                },
                {field:'zwCode',title:'职务编码',width:'49.4%',align:'center'}
            ]],
            toolbar: [{
                iconCls: 'fa fa-plus-circle',
                text:"添加",
                handler: function(){addZw()}
            },{
                iconCls: 'fa fa-trash-o',
                text:"删除",
                handler: function(){deleteZw()}
            }, {
                iconCls: 'fa fa-upload',
                text: "导入",
                handler: function () {
                    uploadZw()
                }
            },{
                iconCls: 'fa fa-download',
                text: "导出",
                handler: function () {
                    exportZw()
                }
            }],
            queryParams:{
                zwName: $("#zwName").val(),
                zwCode: $("#zwCode").val()
            },
            pagination: true,
            pageSize: 10
        });
    };

    var exportZw = function () {
        window.location.href = getZwController() + "exportFile";
    };

    var uploadZw = function () {
        $("#uploadModal").modal();
        $("#uploadModal").on("shown.bs.modal",function(){
//          var url = "http://localhost:8080" + getZwController() + "downloadTemplate";
			var url = "http://localhost:8080/fw/pcReport/exportReport";
            $("#downloadZwExcelTemplate").attr("href",url);
        });

        $('#uploadButton').off("click");
        $("#uploadButton").on("click",function () {
            var uploadinput = document.getElementById("uploadFile");
            if (uploadinput.value == "") {
                Util.alert("上传前请先选择文件!");
                return;
            }
            var formData = new FormData();
            formData.append("file",uploadinput.files[0]);
            $.ajax({
                url: getZwController() + "uploadFile" ,
                data: formData,
                processData: false, //因为data值是FormData对象，不需要对数据做处理。
                contentType: false,
                type: "POST",
                success: function (data) {
                    $('#uploadModal').modal('hide');
                    uploadinput.value=null;
                    Util.alert(data);
                    createZwGrid();
                }
            });
        });
    };

	var formValidator = function(){
		$("#zwForm").validate({
			rules : {
				zwName : {
					required :true,
					maxlength:100
				},
                zwCode : {
                    required :true,
                    maxlength:32,
                    remote: {
                        type: "GET",
                        url: getZwController() + "validateZwCode",
                        data: {  //要传递的参数
                            zwId: function () {
                                return $("#zwForm #zwId").val();
                            },
                            zwCode: function () {
                                return $("#zwForm #zwCode").val();
                            }
                        }
                    }
                },
				sort:{
					digits:true,
					min:1,
					max:255
				},
                description:{
					maxlength:256
				}
			},
			messages : {
                zwName : {
					required:"职务名称不允许为空!",
					maxlength:"职务名称长度不可以超过100"
				},
                zwCode : {
                    required:"职务编码不允许为空!",
                    maxlength:"职务编码长度不可以超过30",
                    remote: "职务编码重复，请重新输入"
                },
				sort:"顺序号必须是小于256的正整数",
                description:"描述长度不可以超过256"
			}
		});
	};
	var getZw = function(id){
		$.ajax({
			url: getZwController() + "/"+id,
			type: "get",
			success: function (data) {
				Tool.deserialize("zwForm", data);
			}
		});
	};

	var addZw = function () {
		var slidebar = Util.slidebar({
			url: zwFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				$("#saveBtn").on("click", function () {
					if($("#zwForm").valid()){
						var data = Tool.serialize("zwForm");
						$.ajax({
							url: getZwController() ,
                            contentType:"application/json",
							data: JSON.stringify(data),
							type: "post",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
                                createZwGrid();
							}
						});
					}
				});
			}
		});
	};
	window._editZw = function(zwId) {
		var slidebar = Util.slidebar({
			url: zwFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				getZw(zwId);
				$("#saveBtn").on("click", function () {
					if($("#zwForm").valid()){
						var data = Tool.serialize("zwForm");
						$.ajax({
							url: getZwController(),
                            contentType:"application/json",
                            data: JSON.stringify(data),
							type: "put",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
								createZwGrid();
							}
						});
					}
				});
			}
		});
	};
	var deleteZw = function() {
		var rows = $("#zwGrid").datagrid("getSelections");
		if (rows == null || rows.length == 0) {
			Util.alert("请选择一行数据!");
			return;
		}
		Util.confirm("是否要删除选中的职务?", function() {
			var ids = "";
			$.each(rows, function(i, row){
				ids += row.zwId + ",";
			});
			ids = ids.substr(0,ids.length - 1);
			$.ajax({
				url: getZwController() ,
				data: {
					ids : ids
				},
				type: "delete",
				success: function (data) {
					createZwGrid();
				}
			});
		}, function() {
			return;
		});

	};

	return {
		init:init
	};
});
